<%-- 
    Document   : search
    Created on : Apr 25, 2012, 5:19:34 PM
    Author     : Reinhard
--%>

<%@page import="java.util.LinkedHashSet"%>
<%@page import="java.util.ArrayList"%>
<%@page import="model.SearchModel"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="/connect.jspf"%>
<%@include file="/header.jsp"%>
<%@page import="model.PostModel, model.TagModel, model.UserModel, model.AchievementModel" %>

<div id="bodycontent">
  <div class="contentcontainer">
    <%
      String input = request.getParameter("q");
      String opt = request.getParameter("search_type");

      int pil = 1;
      if (opt.equals("username")) {
        pil = 2;
      } else if (opt.equals("content")) {
        pil = 3;
      }

      boolean ufound = false;
      boolean cfound = false;

      if (pil == 1 || pil == 2) {
        out.println("<div class='contentbox'>");
        out.println("<div class='title-text'>");
        out.println("<h1>Username search result</h1>");
        out.println("</div>");

        SearchModel sm = new SearchModel(con);
        ResultSet res = sm.searchUsername(input);

        out.println("<ul>");
        while (res.next()) {
          ufound = true;
          out.println("<li>");
          String str = "<a href=profile.jsp?id=" + res.getString("ID") + ">" + res.getString("username") + "</a>";
          out.println(str);
          out.println("</li>");
        }

        if (!ufound) {
          out.println("No result found");
        }

        out.println("</ul>");
        out.println("</div>");
      }

      if (pil == 1 || pil == 3) {
        out.println("<div class='contentbox'>");
        out.println("<div class='title-text'>");
        out.println("<h1>Content search result</h1>");
        out.println("</div>");

        ArrayList<String> resid = new ArrayList<String>();
        SearchModel sm = new SearchModel(con);
        PostModel pm = new PostModel(con);
        TagModel tm = new TagModel(con);

        ResultSet rescon = sm.searchContent(input);
        ResultSet restag = sm.searchTag(input);

        while (rescon.next()) {
          resid.add(rescon.getString("ID"));
        }

        while (restag.next()) {
          resid.add(restag.getString("post_id"));
        }

        LinkedHashSet lhs = new LinkedHashSet();
        lhs.addAll(resid);
        resid.clear();
        resid.addAll(lhs); 
        out.println("<ul>");
        for (String id : resid) {
          ResultSet res = pm.getPost(Integer.parseInt(id));
          while (res.next()) {
            cfound = true;
            out.println("<li>"); 
            out.println("<a href='content.jsp?id=" + id + "'>" + res.getString("post.title") + "</a>");
            out.println("<br />");
            
            String[] desc = res.getString("content").split("\n");
            
            out.println(desc[0]);
            if (desc.length > 1 && desc[1] != null) {
              out.println("<br />" + desc[1]);
            }
            out.println("<br />");
            
            out.println("Tags: ");
            ResultSet restag2 = tm.getTags(id);
            while (restag2.next()) {
              out.println(restag2.getString("type"));
              out.println("&nbsp;");
            }
            out.println("</li>");
          }
        }

        if (!cfound) {
          out.println("No result found");
        }

        out.println("</ul>");
        out.println("</div>");
      }
    %>
  </div>
</div>
<%@include file="/footer.jsp"%>
